package com.shiguo365.market.display.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shiguo365.market.R;
import com.shiguo365.market.bean.Product;

import java.util.List;

/**
 * Created by caomenglong on 16/7/30.
 */
public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {

    private List<Product> productList;

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }




    public ProductListAdapter(List<Product> productList){
        this.productList=productList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_product,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return  vh;
    }

    @Override
    public void onBindViewHolder(final  ViewHolder viewHolder, int position) {
            Product product =productList.get(position);
            ImageLoader imageLoader = ImageLoader.getInstance();
            imageLoader.displayImage(product.getImgurl(), viewHolder.productImage);
            viewHolder.productName.setText(product.getName());
            viewHolder.productCentext.setText(product.getContext());
            viewHolder.productPrice.setText("￥"+Double.toString(product.getPrice()));

            // 如果设置了回调，则设置点击事件
            if (mOnItemClickLitener != null)
            {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        int pos = viewHolder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(viewHolder.itemView, pos);
                    }
                });

                viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener()
                {
                    @Override
                    public boolean onLongClick(View v)
                    {
                        int pos = viewHolder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(viewHolder.itemView, pos);
                        return false;
                    }
                });
            }
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView productImage;
        public TextView productName;
        public TextView productCentext;
        public TextView productPrice;

        public ViewHolder(View view){
            super(view);
            productImage=(ImageView) view.findViewById(R.id.product_imageview);
            productName= (TextView) view.findViewById(R.id.product_name);
            productCentext=(TextView) view.findViewById(R.id.product_context);
            productPrice =(TextView) view.findViewById(R.id.product_price);
        }


    }
}
